package com.oa.role.DaoImpl;

import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.springframework.orm.hibernate3.HibernateTemplate;

import com.oa.entity.TblRight;
import com.oa.entity.TblRole;
import com.oa.role.Dao.RoleDao;
/**
 * 角色数据操作接口
 * @author Bond
 * @date 2015年10月10日 11:36:51
 */

public class RoleDaoImpl implements RoleDao {
	
	private HibernateTemplate hibernateTemplate;
	
	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}
	/*
	 * 角色加载
	 * @see com.oa.role.Dao.RoleDao#load(java.lang.Integer)
	 */
	@Override
	public TblRole load(Integer Id) {
		// TODO Auto-generated method stub
		TblRole tr = new TblRole();
		try{
			tr = (TblRole) this.hibernateTemplate.load(TblRole.class, Id);
		}catch(Exception e){
			e.printStackTrace();
		}
		return tr;
	}
	/*
	 * 权限加载
	 * @see com.oa.role.Dao.RoleDao#load(java.lang.String)
	 */
	@Override
	public TblRight load(String rights_code) {
		// TODO Auto-generated method stub
		TblRight tr = new TblRight();
		try {
			tr = (TblRight) this.hibernateTemplate.load(TblRight.class, rights_code);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return tr;
	}
	/*
	 * 角色增加
	 * @see com.oa.role.Dao.RoleDao#add(com.oa.entity.TblRole)
	 */
	@Override
	public boolean add(TblRole role) {
		// TODO Auto-generated method stub
		boolean flag = false;
		try{
			this.hibernateTemplate.save(role);
			flag = true;
		}catch(Exception e){
			e.printStackTrace();
		}
		return flag;
	}
	/*
	 * 角色删除
	 * @see com.oa.role.Dao.RoleDao#delete(com.oa.entity.TblRole)
	 */
	@Override
	public boolean delete(TblRole role) {
		// TODO Auto-generated method stub
		boolean flag = false;
		try{
			this.hibernateTemplate.delete(role);
			flag = true;
		}catch(Exception e){
			e.printStackTrace();
		}
		return flag;
	}
	/*
	 * 角色更新
	 * @see com.oa.role.Dao.RoleDao#update(com.oa.entity.TblRole)
	 */
	@Override
	public boolean update(TblRole role) {
		// TODO Auto-generated method stub
		boolean flag = false;
		try{
			this.hibernateTemplate.update(role);
			flag = true;
		}catch(Exception e){
			e.printStackTrace();
		}
		return flag;
	}
	/*
	 * 数据查询
	 * @see com.oa.role.Dao.RoleDao#searchData(java.lang.String, java.util.Map)
	 */
	@Override
	public List searchData(String hql, Map params) {
		// TODO Auto-generated method stub
		Query query = null;
		List list = null;
		try{
			if(params!=null&&params.size()>0){
				query = this.hibernateTemplate.getSessionFactory().openSession().createQuery(hql);
				Object[] keyArrs = params.keySet().toArray();
				for(int i=0;i<keyArrs.length;i++){
					query.setParameter(keyArrs[i].toString(), params.get(keyArrs[i]));
				}
				list = query.list();
			}else{
				list = this.hibernateTemplate.find(hql);
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return list;
	}
}
